import {
  SET_UNPACK_LIST,
  SET_UNPACK_DETAILS,
  CLEAR_UNPACK_DETAILS
} from "./action-types";

const initialState = {
  unpackList: [],
  unpackListOffset: 0,

  details: null
};

const handleUnpackList = (state, payload) => {
  let { items, offset } = payload;
  if (items.length === 0 && offset === 0) {
    return { ...state, unpackList: [], unpackListOffset: 0 };
  } else {
    return {
      ...state,
      unpackList: state.unpackList.concat(items),
      unpackListOffset: offset
    };
  }
};

const data = (state = initialState, action) => {
  switch (action.type) {
    case SET_UNPACK_LIST:
      return handleUnpackList(state, action.payload);
    case SET_UNPACK_DETAILS:
      return { ...state, details: action.payload.item };
    case CLEAR_UNPACK_DETAILS:
      return { ...state, details: null };
    default:
      return state;
  }
};

export default data;
